<template>
	<view class="safe-area-inset-bottom">
		<uni-collapse ref="collapse" v-model="activeVal">
			<!-- 签约设置 -->
			<uni-collapse-item title="签约设置">
				<view class="content">
					<uni-section class="mb-10" title="内容阅读" sub-title="若开启，签署人需要确认”阅读并知晓合同内容”的行为意愿后才可进行后续操作。"
						style="margin-top: 0;">
						<template v-slot:right>
								<switch color="#00a660" style="transform:scale(0.7)" @click="handleContractReadStatusStatusChange(notice)"
									:checked="notice.contractReadStatus == 1 ?true:false" v-model="notice.smsNotice" />
						</template>
					</uni-section>
				</view>
			</uni-collapse-item>
			<!-- 通知设置 -->
			<uni-collapse-item title="通知设置">
				<!-- 站内通知 -->
				<uni-section class="mb-10" title="站内通知" sub-title="若关闭，进行文件签署、文件签署完成时，将不通过站内信的方式向您发送通知信息。"
					style="margin-top: 0;">
					<template v-slot:right>
							<switch color="#00a660" style="transform:scale(0.7)" disabled v-if="notice.sysNotice == 1"
								:checked="notice.sysNotice == 1 ?true:false" v-model="notice.sysNotice" />
							<switch color="#00a660" style="transform:scale(0.7)" @click="handleSysNoticeStatusChange(notice)" v-else
								:checked="notice.sysNotice == 1 ?true:false" v-model="notice.sysNotice" />
					</template>
				</uni-section>
				<!-- 短信通知 -->
				<uni-section class="mb-10" title="短信通知" sub-title="若关闭，进行文件签署、文件签署完成时，将不通过短信的方式向您注册的手机发送通知短信。">
					<template v-slot:right>
							<switch color="#00a660" style="transform:scale(0.7)" disabled v-if="notice.smsNotice == 1"
								:checked="notice.smsNotice == 1 ?true:false" v-model="notice.smsNotice" />
							<switch color="#00a660" style="transform:scale(0.7)" @click="handleSmsNoticeStatusChange(notice)" v-else
								:checked="notice.smsNotice == 1 ?true:false" v-model="notice.smsNotice" />
					</template>
				</uni-section>
				<!-- 邮箱通知 -->
				<uni-section class="mb-10" title="邮箱通知" sub-title="若关闭，进行文件签署、文件签署完成时，将不通过邮件的方式向您注册的邮箱发送通知邮件。">
					<template v-slot:right>
							<switch color="#00a660" style="transform:scale(0.7)" @click="handleEmailNoticeStatusChange(notice)"
								:checked="notice.emailNotice == 1 ?true:false" v-model="notice.emailNotice" />
					</template>
				</uni-section>
				<!-- 微信通知 -->
				<uni-section class="mb-10" title="微信通知" sub-title="若关闭，进行文件签署、文件签署完成时，将不通过微信的方式向您关注的公众号发送通知信息。">
					<template v-slot:right>
							<switch color="#00a660" style="transform:scale(0.7)" @click="handleWechatNoticeStatusChange(notice)"
								:checked="notice.wechatNotice == 1 ?true:false" v-model="notice.wechatNotice" />
					</template>
				</uni-section>
			</uni-collapse-item>
			<!-- 提醒设置 -->
			<uni-collapse-item title="提醒设置">
				<!-- 签署截止日期 -->
				<uni-section class="mb-10" style="margin-top: 0;" title="签署截止日期">
					<template v-slot="subTitle">
						<view class="row-text">
							<span>提前</span>
							<uni-easyinput v-model="signEndDays" size="mini" maxlength="3"
								:disabled="notice.signEndNotice == 0? true : false"
								@blur="updateSignEndDays(signEndDays)"
								style="width:120rpx;padding:0 10rpx;display: inline-block;" :clearable="false">
							</uni-easyinput>
							<span>天，提醒您参与签署的文件即将超出签署截止日期。</span>
						</view>
					</template>
					<template v-slot:right>
							<switch color="#00a660" style="transform:scale(0.7)" @click="handleSignEndNoticeStatusChange(notice)"
								:checked="notice.signEndNotice == 1 ?true:false" v-model="notice.signEndNotice" />
					</template>
				</uni-section>
				<uni-section class="mb-10" title="合同到期提醒">
					<template v-slot="subTitle">
						<view class="row-text">
							<span>提前</span>
							<uni-easyinput v-model="contractExpireDays" size="mini" maxlength="3"
								:disabled="notice.contractExpireNotice == 0? true : false"
								@blur="updateContractExpireDays(contractExpireDays)"
								style="width:120rpx;padding:0 10rpx;display: inline-block;" :clearable="false">
							</uni-easyinput>
							<span>天，提醒您参与签署的文件即将超出签署截止日期。</span>
						</view>
					</template>
					<template v-slot:right>
							<switch color="#00a660" style="transform:scale(0.7)" @click="handleContractExpireNoticeStatusChange(notice)"
								:checked="notice.contractExpireNotice == 1 ?true:false"
								v-model="notice.contractExpireNotice" />
					</template>
				</uni-section>
			</uni-collapse-item>
			<!-- 提醒设置 -->
			<uni-collapse-item title="合同打印/下载设置">
				<!-- 打印权限 -->
				<uni-section class="mb-10" style="margin-top: 0;" title="打印权限">
					<template v-slot="subTitle">
						<view class="row-text" style="padding-right:40rpx">
						<checkbox-group @change="handleContractPrintTypeChange" v-model="notice.contractPrintType">
							<label v-for="item in checkListPrint" :key="item.value" style="padding-left: 40rpx;">
								<checkbox :value="item.value" :checked="item.checked" color="#00a660" style="transform:scale(0.6)"/>
								{{item.text}}
							</label>	
						</checkbox-group>				
						</view>
					</template>
				</uni-section>
				<!-- 下载权限 -->
				<uni-section class="mb-10" title="下载权限">
					<template v-slot="subTitle">
						<view class="row-text" style="padding-right:40rpx">
						  <checkbox-group @change="handleContractDownloadTypeChange" v-model="notice.contractDownloadType">
						  	<label v-for="item in checkListDown" :key="item.value" style="padding-left: 40rpx;">
						  		<checkbox :value="item.value" :checked="item.checked" color="#00a660" style="transform:scale(0.6)"/>
						  		{{item.text}}
						  	</label>	
						  </checkbox-group>			
						</view>
					</template>
				</uni-section>
			</uni-collapse-item>
		</uni-collapse>
		<!-- 支付设置 -->
		<view class="pay_view" @click="payset">
			<span>支付设置</span>
			<uni-icons type="forward" size="14"></uni-icons>
		</view>
	</view>
</template>

<script>
	import {getCompanyInfo,update} from "@/api/ec/console/busiConfig/systemSet";
	export default {
		data() {
			return {
				// 默认的打印权限
				permission:[{
					text: '合同经办人',
					value: '合同经办人',
					checked:true,
				}, {
					text: '合同签署人',
					value: '合同签署人',
					checked:true,
				}, {
					text: '合同抄送人',
					value: '合同抄送人',
					checked:true,
				}, {
					text: '外部查阅人',
					value: '外部查阅人',
					checked:true,
				}], 
				//默认的下载权限
				downloadVal: [{
					text: '合同经办人',
					value: '合同经办人',
					checked:true,
				}, {
					text: '合同签署人',
					value: '合同签署人',
					checked:true,
				}, {
					text: '合同抄送人',
					value: '合同抄送人',
					checked:false,
				}, {
					text: '外部查阅人',
					value: '外部查阅人',
					checked:false,
				}],
				//打印
				checkListPrint: [{
					text: '合同经办人',
					value: '合同经办人',
					checked:false,
				}, {
					text: '合同签署人',
					value: '合同签署人',
					checked:false,
				}, {
					text: '合同抄送人',
					value: '合同抄送人',
					checked:false,
				}, {
					text: '外部查阅人',
					value: '外部查阅人',
					checked:false,
				}], 
				// 下载
				checkListDown: [{
					text: '合同经办人',
					value: '合同经办人',
					checked:false,
				}, {
					text: '合同签署人',
					value: '合同签署人',
					checked:false,
				}, {
					text: '合同抄送人',
					value: '合同抄送人',
					checked:false,
				}, {
					text: '外部查阅人',
					value: '外部查阅人',
					checked:false,
				}], 
				activeVal: ['0'],
				// 系统设置参数
				notice: {},
				// 签署截止日期
				signEndDays: 0,
				// 合同到期提醒
				contractExpireDays: 0
			}
		},
		created() {
			this.getInfoDetail();
		},
		methods: {
			/** 支付设置 */
			payset() {
				this.$tab.navigateTo('./paymentSetting')
			},
			/** 查询消息设置信息 */
			getInfoDetail() {
                this.$modal.loading();
				getCompanyInfo().then(response => {
					uni.hideLoading()
					this.notice = response.data;
					this.signEndDays = response.data.signEndDays;
					this.contractExpireDays = response.data.contractExpireDays;
					
					// 合同下载默认设置 
					if (this.notice.contractDownloadType == null) {
						this.checkListDown = this.downloadVal;
						console.log(this.checkListDown)
					} else {
						this.checkListDown.forEach(flow => {
						JSON.parse(this.notice.contractDownloadType).forEach(item => {
							if(flow.text.includes(item)){
								flow.checked = true
							}	
						})
						})
					}
					// 合同打印默认设置
					if (this.notice.contractPrintType == null) {
						this.checkListPrint = this.permission;
					} else {
						this.checkListPrint.forEach(flow => {
						JSON.parse(this.notice.contractPrintType).forEach(item => {
							if(flow.text.includes(item)){
								flow.checked = true
							}	
						})
						})
					}
				});
			},
		    /** 设置签署截止日期 */
		    updateSignEndDays(signEndDays) {
			  if (signEndDays <= 0) {
				  this.signEndDays = this.notice.signEndDays;
				  this.$modal.msgError("不能为空，最小是1！");
				  return;
				}
		      if (signEndDays.length <= 0) {
		        this.signEndDays = this.notice.signEndDays;
		        return;
		      }
		      if (parseInt(signEndDays) == this.notice.signEndDays) {
		        return;
		      }
			  // 
		      let id = this.notice.id;
		      const param = {id, signEndDays}
		      update(param).then(() => {
		        this.notice.signEndDays = parseInt(signEndDays);
				this.getInfoDetail();
		        this.$modal.msgSuccess("签署截止日期设置成功！");
		      }).catch(() => {
		        this.signEndDays = this.notice.signEndDays;
		      });
		    },
		    /** 设置合同到期提醒日期 */
		    updateContractExpireDays(contractExpireDays) {
		      if (contractExpireDays.length <= 0) {
		        this.contractExpireDays = this.notice.contractExpireDays;
				this.$modal.msgError("不能为空，最小是 1 ！");
		        return;
		      }
		      if (parseInt(contractExpireDays) == this.notice.contractExpireDays) {
		        return;
		      }
		      let id = this.notice.id;
		      const param = {id, contractExpireDays}
		      update(param).then(() => {
		        this.notice.contractExpireDays = parseInt(contractExpireDays);
		        this.$modal.msgSuccess("合同到期提醒日期设置成功！");
		      }).catch(() => {
		        this.contractExpireDays = this.notice.contractExpireDays;
		      });
		    },
			/** 签约设置 */
			handleContractReadStatusStatusChange(data) {
				let msg = (data.contractReadStatus === 0 ? "开启" : "关闭") + "合同内容阅读";
				let id = data.id;
				let contractReadStatus = data.contractReadStatus === 1 ? 0 : 1;
				const param = {id,contractReadStatus}
				update(param).then(() => {
					this.$modal.msgSuccess(msg + "成功！");
				}).catch(function() {
					data.contractReadStatus = data.contractReadStatus === 1 ? 0 : 1;
				});
			},
			/** 站内通知  */
			handleSysNoticeStatusChange(data) {
				let msg = (data.sysNotice === 0 ? "开启" : "关闭") + "站内通知";
				let id = data.id;
				let sysNotice = data.sysNotice === 1 ? 0 : 1;
				const param = {id,sysNotice}
				update(param).then(() => {
						this.$modal.msgSuccess(msg + "成功！");
				}).catch(function() {
					data.sysNotice = data.sysNotice === 1 ? 0 : 1;
				});
			},
			/** 短信通知 */
			handleSmsNoticeStatusChange(data) {
				let msg = (data.smsNotice === 0 ? "开启" : "关闭") + "短信通知";
				let id = data.id;
				let smsNotice = data.smsNotice === 1 ? 0 : 1;
				const param = {id,smsNotice}
				update(param).then(() => {
					this.$modal.msgSuccess(msg + "成功！");
				}).catch(function() {
					data.smsNotice = data.smsNotice === 1 ? 0 : 1;
				});
			},
			/** 邮箱通知 */
			handleEmailNoticeStatusChange(data) {
				let msg = (data.emailNotice === 0 ? "开启" : "关闭") + "邮箱通知";
				let id = data.id;
				let emailNotice = data.emailNotice === 1 ? 0 : 1;
				const param = {id,emailNotice}
				update(param).then(() => {
						this.$modal.msgSuccess(msg + "成功！");
				}).catch(function() {
					data.emailNotice = data.emailNotice === 1 ? 0 : 1;
				});
			},
			/** 微信通知 */
			handleWechatNoticeStatusChange(data) {
				let msg = (data.wechatNotice === 0 ? "开启" : "关闭") + "微信通知";
				let id = data.id;
				let wechatNotice = data.wechatNotice === 1 ? 0 : 1;
				const param = {id,wechatNotice}
				update(param).then(() => {
					this.$modal.msgSuccess(msg + "成功！");
				}).catch(function() {
					data.wechatNotice = data.wechatNotice === 1 ? 0 : 1;
				});
			},
			/** 签署截止日期 */
			handleSignEndNoticeStatusChange(data) {
				let msg = (data.signEndNotice === 0 ? "开启" : "关闭") + "签署截止日期提示";
				let id = data.id;
				let signEndNotice = data.signEndNotice === 1 ? 0 : 1;
				const param = {id,signEndNotice}
				update(param).then(() => {
					this.$modal.msgSuccess(msg + "成功！");
					this.getInfoDetail();
				}).catch(function() {
				    data.signEndNotice = data.signEndNotice === 1 ? 0 : 1;
				});
			},
			/** 合同到期提醒日期 */
			handleContractExpireNoticeStatusChange(data) {
				let msg = (data.contractExpireNotice === 0 ? "开启" : "关闭") + "合同到期提醒日期提示";
				let id = data.id;
				let contractExpireNotice = data.contractExpireNotice === 1 ? 0 : 1;
				const param = {id,contractExpireNotice}
				update(param).then(() => {
					this.$modal.msgSuccess(msg + "成功！");
					this.getInfoDetail();
				}).catch(function() {
                  data.contractExpireNotice = data.contractExpireNotice === 1 ? 0 : 1;
				});
			},
			// 合同下载设置
			handleContractDownloadTypeChange(e){
			  update({id:this.notice.id,contractDownloadType:JSON.stringify(e.detail.value)}).then(() => {
			    this.$modal.msgSuccess("下载权限设置成功！");
			  })
			},
			// 合同打印设置
			handleContractPrintTypeChange(e){
			  update({id:this.notice.id,contractPrintType:JSON.stringify(e.detail.value)}).then(() => {
			    this.$modal.msgSuccess("打印权限设置成功！");
			  })
			},
		}
	}
</script>

<style lang="scss" scoped>
	.title_row {
		font-size: 28rpx;

		.sub_tit {
			font-size: 20rpx;
		}
	}

	::v-deep .uni-data-checklist .checklist-group .checklist-box {
		margin-left: 65rpx;
		margin-bottom: 20rpx;
	}

	// 折叠面板箭头修改
	::v-deep .uniui-forward {
		color: #000 !important;
	}

	// 折叠面板修改
	::v-deep .uni-collapse-item__wrap {
		background-color: #f5f6f7;
	}

	::v-deep .uni-collapse-item__title {
		background-color: #f7f8fa;
	}

	::v-deep .uni-collapse-item__title-box {
		background-color:#f7f8fa;
	}

	::v-deep .uni-section .uni-section-header {
		padding: 24rpx 32rpx;
	}

	::v-deep .uni-noticebar {
		margin: 0;
		padding: 14rpx 20rpx;
	}

	::v-deep .uni-easyinput__content-input {
		height: 40rpx;
	}

	.mb-10 {
		margin-top: 30rpx;
	}

	page {
		background-color: $uni-bg-color;
	}

	.row-text {
		padding: 0 40rpx 20rpx 40rpx;
		font-size: 22rpx;
		color: #999999;
	}
    // 支付设置
	.pay_view {
		padding: 20rpx 24rpx 20rpx 30rpx;
		background-color: #f7f8fa;
		display: flex;
		justify-content: space-between;
		span{
			font-weight: 500;
			color: #000;
		}
		.uni-icons{
			font-weight: bold;
			color: rgb(102, 102, 102);
		}
	}
</style>
